package com.xiaoxu.intranetweb.mybatisplusSzappdb.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.entity.SysUser;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.entity.TGypsumPlan;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.entity.TTruckBlacklist;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.mapper.TTruckBlacklistMapper;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.service.ITTruckBlacklistService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiaoxu.intranetweb.util.StringUtils;
import com.xiaoxu.intranetweb.util.SzResult;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.TextStyle;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xiaoxu
 * @since 2024-07-08
 */
@Service
public class TTruckBlacklistServiceImpl extends ServiceImpl<TTruckBlacklistMapper, TTruckBlacklist> implements ITTruckBlacklistService {

    @Override
    public SzResult getAllTruck(Integer page, Integer pageSize, String truckNo, String startTime, String endTime, String plantId) {
        QueryWrapper<TTruckBlacklist> truckBlacklistQueryWrapper = new QueryWrapper<>();
        truckBlacklistQueryWrapper.like(StringUtils.isNotEmpty(truckNo),"TRUCK_NO",truckNo);
        truckBlacklistQueryWrapper.ge(StringUtils.isNotEmpty(startTime),"END_TIME",startTime);
        truckBlacklistQueryWrapper.le(StringUtils.isNotEmpty(endTime),"END_TIME",endTime);
//        truckBlacklistQueryWrapper.ne("DEL_FLAG",1);
        Page<TTruckBlacklist> Page = this.page(new Page<>(page, pageSize), truckBlacklistQueryWrapper);
        Map<String, Object> map = new HashMap<>();
        map.put("list",Page.getRecords());
        map.put("total", Page.getTotal());
        return SzResult.success(map);
    }

    @Override
    public SzResult addTruck(TTruckBlacklist truckBlacklist, SysUser user) {
        if (StringUtils.isNotEmpty(truckBlacklist.getId())){
            QueryWrapper<TTruckBlacklist> truckBlacklistQueryWrapper = new QueryWrapper<>();
            truckBlacklistQueryWrapper.eq("TRUCK_ID",truckBlacklist.getId());
            if (this.getOne(truckBlacklistQueryWrapper) != null) {
                return SzResult.error("已有重复车辆");
            }
        }
        if (this.save(truckBlacklist)) {
            return SzResult.success("添加成功");
        }
        return SzResult.error("添加失败");
    }

    @Override
    public SzResult updateTruck(TTruckBlacklist truckBlacklist, SysUser user) {
        if (this.updateById(truckBlacklist)) {
            return SzResult.success("修改成功");
        }
        return SzResult.error("修改失败");
    }

    @Override
    public SzResult delTruck(String id) {
        if (this.removeById(id)) {
            return SzResult.success("删除成功");
        }
        return SzResult.error("删除失败");
    }
}
